class Solution {
public:
    vector<int> canSeePersonsCount(vector<int>& heights) {
        vector<int> res(heights.size(), 0), stack({0});
        for (int i = 1; i < heights.size(); ++i) {
            while (not stack.empty() and heights[i] >= heights[stack.back()]) {
                res[stack.back()] += 1;
                stack.pop_back();
            }
            if (not stack.empty()) {
                res[stack.back()] += 1;
            }
            stack.emplace_back(i);
        }
        return res;
    }
};
